package com.kkkkkba.search.dataSource;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kkkkkba.search.model.entity.Video;
import com.kkkkkba.search.common.ErrorCode;
import com.kkkkkba.search.exception.BusinessException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 视频服务实现类
 */
@Service
public class VideoDataSource implements DataSource<Video>{

    public Page<Video> doSearch(String searchText, long pageNum, long pageSize) {
        long current = (pageNum - 1) * pageSize + 1;
        if (current == 1) {
            pageNum = 1;
            pageSize = 10;
            current = 10;
        }
        if (searchText == null || searchText.equals("")) {
            searchText = "Java之父";
        }
        String url = String.format(
                "https://api.bilibili.com/x/web-interface/search/all/v2?keyword=%s3&page=%s",
                searchText,
                current);
        String cookieStr = "nostalgia_conf=-1; b_lsid=628EB3F5_18870B73AA2; _uuid=12696587-83F7-4B5E-9B102-5D69A2EB1109F36332infoc; buvid_fp=c9eaf4909d59079e2076b2a8fafc3433; buvid3=E12B27E9-9DB8-A7E7-B27D-16BFBE657F9436476infoc; b_nut=1685518237; buvid4=C3FF18F9-D3A3-28E9-9EF2-4191E696DEBF07763-023053114-QvnVcBMEUB+jRtrufLun47YWx9OscNaP7e+IhVSceUKVzhy4UcdXiA%3D%3D";
        Map<String, String> cookieMap = new HashMap<>();
        for (String cookie : cookieStr.split("; ")) {
            String[] nameValue = cookie.split("=");
            cookieMap.put(nameValue[0], nameValue[1]);
        }
        List<Video> list = new ArrayList<>();
        Document doc = null;
        try {
            doc = Jsoup.connect(url)
                    .cookies(cookieMap)
                    .header("Content-Type", "application/json")
                    .header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57")
                    .ignoreContentType(true)
                    .get();
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"数据获取异常");
        }
        String s = doc.select("body").text();
        JSONObject json = new JSONObject(s);
        json = new JSONObject(json.get("data"));
        JSONObject obj = new JSONObject(json);
        JSONArray type = obj.getJSONArray("result");
        Object o = type.get(type.size() - 1);
        json = new JSONObject(o);
        JSONArray video = json.getJSONArray("data");


        for (Object i : video) {
            JSONObject v = new JSONObject(i);
            String title = (String) v.get("title");
//            title = title.replaceAll("<.+?>", "");
            String arcurl = (String) v.get("arcurl");
            String pic ="http:"+ v.get("pic");
            String tag = (String) v.get("tag");
            String author = (String) v.get("author");

//            System.out.println(title+"" +
//                    "\n" + arcurl +
//                    "\n" + pic +
//                    "\n" +tag);
            list.add(new Video(title,arcurl,pic,tag,author));
        }
        Page<Video> videoPage = new Page<>(pageNum,pageSize);
        videoPage.setRecords(list);
        return videoPage;
    }

}
